package com.pospi.pai.yunpos.report;

import android.app.DatePickerDialog;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.DialogFragment;
import android.text.format.DateFormat;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jzxiang.pickerview.TimePickerDialog;
import com.jzxiang.pickerview.data.Type;
import com.jzxiang.pickerview.listener.OnDateSetListener;
import com.lany.sp.SPHelper;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.mylhyl.circledialog.CircleDialog;
import com.mylhyl.circledialog.params.ProgressParams;
import com.pospi.callbacklistener.HttpCallBackListener;
import com.pospi.http.HttpConnection;
import com.pospi.pai.yunpos.R;
import com.pospi.pai.yunpos.base.Base2Activity;
import com.pospi.pai.yunpos.been.CustomerBeen;
import com.pospi.pai.yunpos.been.SaveOrderInfo;
import com.pospi.pai.yunpos.been.SupBeen;
import com.pospi.pai.yunpos.caigou.SupActivity;
import com.pospi.pai.yunpos.cash.PointActivity;
import com.pospi.pai.yunpos.login.Constant;
import com.pospi.pai.yunpos.util.BillUtil;
import com.pospi.util.GetData;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import lib.kingja.switchbutton.SwitchMultiButton;

public class GoodsReportActivity extends Base2Activity {
    @Bind(R.id.total_je)
    TextView total_je;
    @Bind(R.id.bill_num)
    TextView bill_num;
    @Bind(R.id.switch_button)
    SwitchMultiButton switch_button;
    @Bind(R.id.tv_list_title)
    TextView tv_list_title;
    @Bind(R.id.bill_num_title)
    LinearLayout bill_num_title;
    @Bind(R.id.djsl_title)
    TextView djsl_title;

    @Bind(R.id.bm)
    EditText bm;
    @Bind(R.id.tv_fl)
    TextView tv_fl;
    @Bind(R.id.tm)
    EditText tm;
    @Bind(R.id.tv_pp)
    TextView tv_pp;
    @Bind(R.id.mc)
    EditText mc;
    @Bind(R.id.tv_md)
    TextView tv_md;
    @Bind(R.id.tv_gys)
    TextView tv_gys;
    @Bind(R.id.zjm)
    EditText zjm;
    @Bind(R.id.time_start_select)
    TextView time_start_select;
    @Bind(R.id.time_end_select)
    TextView time_end_select;

    private String count;
    private String je;
    private MyAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentLayout(R.layout.activity_goods_report);
        ButterKnife.bind(this);
        setTitle("商品TOP排行");
        setMenuImage(R.drawable.icon_search_white);
        time_start_select.setText(GetData.getYYMMDDTime());
        time_end_select.setText(GetData.getYYMMDDTime());
        init();
//        downStore();
        downSup();
        downCus();
        down();
        downmd();
        downpp();

    }

    @Override
    public void onMenuClick(View view) {
        super.onMenuClick(view);
//        Intent intent = new Intent(this, GoodsReportSearchActivity.class);
//        startActivity(intent);

    }

    List<String> strings = new ArrayList<>();

    @OnClick({R.id.tv_fl, R.id.tv_pp, R.id.tv_md, R.id.tv_gys, R.id.bt_search, R.id.time_start_select, R.id.time_end_select})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_fl:
                showfl(tv_fl);
                break;
            case R.id.tv_pp:
                showpp(tv_pp);
                break;
            case R.id.tv_md:
                showmd(tv_md);
                break;
            case R.id.bt_search:
                searchBillfast();
                switch_button.clearSelection();
                break;
            case R.id.tv_gys:
                Intent intent = new Intent(this, SupActivity.class);
                intent.putExtra("mark", "GoodsReportSearchActivity");
                startActivity(intent);
                break;
            case R.id.time_start_select:
                showDate(time_start_select);
//                TimePickerDialog dialogYearMonthDay = new TimePickerDialog.Builder()
//                        .setType(Type.YEAR_MONTH_DAY)
//                        .setTitleStringId("选择日期")
//                        .setWheelItemTextSize(16)
//                        .setThemeColor(getResources().getColor(R.color.blue_cannot_click))
//                        .setCallBack(new OnDateSetListener() {
//                            @Override
//                            public void onDateSet(TimePickerDialog timePickerView, long millseconds) {
//                                Date date = new Date(millseconds);
//                                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//                                String time = format.format(date);
//                                time_start_select.setText(time);
//                            }
//                        })
//                        .build();
//                dialogYearMonthDay.show(getSupportFragmentManager(), "YEAR_MONTH_DAY");
                break;
            case R.id.time_end_select:
                showDate(time_end_select);
//                TimePickerDialog dialogYearMonthDay2 = new TimePickerDialog.Builder()
//                        .setType(Type.YEAR_MONTH_DAY)
//                        .setTitleStringId("选择日期")
//                        .setThemeColor(getResources().getColor(R.color.blue_cannot_click))
//                        .setWheelItemTextSize(16)
//                        .setCallBack(new OnDateSetListener() {
//                            @Override
//                            public void onDateSet(TimePickerDialog timePickerView, long millseconds) {
//                                Date date = new Date(millseconds);
//                                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//                                String time = format.format(date);
//                                time_end_select.setText(time);
//                            }
//                        })
//                        .build();
//                dialogYearMonthDay2.show(getSupportFragmentManager(), "YEAR_MONTH_DAY");
                break;
        }
    }

    private void init() {
        switch_button.setText("今天", "昨天", "近三天", "近七天", "近一月");
        switch_button.setOnSwitchListener(new SwitchMultiButton.OnSwitchListener() {
            @Override
            public void onSwitch(int position, String tabText) {
                switch (position) {
                    case 0:
                        startrq = GetData.getAnyTime(0);
                        endrq = GetData.getAnyTime(0);
                        time_start_select.setText(startrq);
                        time_end_select.setText(endrq);
                        searchBillfast();


                        break;
                    case 1:
                        startrq = GetData.getAnyTime(1);
                        endrq = GetData.getAnyTime(1);
                        time_start_select.setText(startrq);
                        time_end_select.setText(endrq);
                        searchBillfast();

                        break;
                    case 2:
                        startrq = GetData.getAnyTime(3);
                        endrq = GetData.getAnyTime(0);
                        time_start_select.setText(startrq);
                        time_end_select.setText(endrq);
                        searchBillfast();
                        break;
                    case 3:
                        startrq = GetData.getAnyTime(7);
                        endrq = GetData.getAnyTime(0);
                        time_start_select.setText(startrq);
                        time_end_select.setText(endrq);
                        searchBillfast();
                        break;
                    case 4:
                        startrq = GetData.getAnyTime(30);
                        endrq = GetData.getAnyTime(0);
                        time_start_select.setText(startrq);
                        time_end_select.setText(endrq);
                        searchBillfast();
                        break;


                }
            }
        });
        tv_list_title.setText("没有查询到数据");

        ListView bill_list = (ListView) findViewById(R.id.list_bill);
        adapter = new MyAdapter();
        bill_list.setAdapter(adapter);

        searchBillfast();

    }

    private String market = "";
    private String supid = "";
    private String custid = "";
    private String brandid = "";
    private String num = "";
    private String barcode = "";
    private String name = "";
    private String cat = "";

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
//        switch_button.clearSelection();
//        startrq = intent.getStringExtra("startrq");
//        endrq = intent.getStringExtra("endrq");
//        market = intent.getStringExtra("market");
//        supid = intent.getStringExtra("supid");
//
//        custid = intent.getStringExtra("custid");
//        brandid = intent.getStringExtra("brandid");
//        num = intent.getStringExtra("num");
//        barcode = intent.getStringExtra("barcode");
//        name = intent.getStringExtra("name");
//        searchBillfast();
        SupBeen been = (SupBeen) intent.getSerializableExtra("sup");
        if (been != null) {
            tv_gys.setText(been.getName());
            supid = been.getId();

        }

    }

    private class MyAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return topBeens.size();
        }

        @Override
        public Object getItem(int i) {
            return topBeens.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            Holder holder;
            if (view == null) {
                holder = new Holder();
                view = LayoutInflater.from(GoodsReportActivity.this).inflate(R.layout.goods_report_item, null);
                holder.xh = view.findViewById(R.id.tv_xh);
                holder.name = view.findViewById(R.id.tv_name);
                holder.xsje = view.findViewById(R.id.tv_xsje);
                holder.xssl = view.findViewById(R.id.tv_xssl);
                holder.mle = view.findViewById(R.id.tv_xsmle);
                holder.mlr = view.findViewById(R.id.tv_xsmlr);
                view.setTag(holder);
            } else {
                holder = (Holder) view.getTag();
            }
            GoodsTopBeen been = topBeens.get(i);
            holder.xh.setText(i + 1 + "");
            holder.name.setText(been.getName() + "[" + been.getNum() + "]");
            holder.xsje.setText(been.getLSJJE());
            holder.xssl.setText(been.getSL());
            holder.mle.setText(been.getMLE());
            holder.mlr.setText(been.getMLL());

            return view;
        }

        class Holder {
            TextView xh;
            TextView name;
            TextView xsje;
            TextView xssl;
            TextView mle;
            TextView mlr;

        }
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    closeLodindDialog();
                    if (topBeens.size() == 0) {
                        setbill(false);
                        adapter.notifyDataSetChanged();
                    } else {
                        setbill(true);
                        adapter.notifyDataSetChanged();
                    }

                    break;
                case 2:

                    break;
                case 3:

                    break;
                case 4:

                    break;
            }

        }
    };

    private void setbill(boolean o) {
        if (o) {
            bill_num_title.setVisibility(View.VISIBLE);
            tv_list_title.setText("总销售额：");
            total_je.setText(je);
            bill_num.setText("" + count);
        } else {
            tv_list_title.setText("没有查询到数据");
            total_je.setText("");
            bill_num.setText("");
            bill_num_title.setVisibility(View.INVISIBLE);
            topBeens.clear();
            adapter.notifyDataSetChanged();
        }

    }


    //下载门店
    private void downStore() {
        Map<String, String> params = new HashMap<>();
        params.put("logid", SPHelper.getInstance().getString(Constant.USER_ID));
        params.put("sysid", SPHelper.getInstance().getString(Constant.SYS_ID));
        params.put("model", "base.mselect");
        params.put("fun", "getlastmarket");
        Log.i("goods", params.toString());
        new HttpConnection().postNet(params, new HttpCallBackListener() {
            @Override
            public void CallBack(String Response) {
                Log.i("login", Response);
                try {
                    JSONObject jsonObject = new JSONObject(Response);
                    JSONArray result = jsonObject.getJSONArray("result");
                    SPHelper.getInstance().putString(Constant.STORE_LIST, result.toString());
                } catch (JSONException e) {

                    e.printStackTrace();
                }
            }
        });


    }

    //下载供应商
    private void downSup() {
        Map<String, String> params = new HashMap<>();
        params.put("logid", SPHelper.getInstance().getString(Constant.USER_ID));
        params.put("sysid", SPHelper.getInstance().getString(Constant.SYS_ID));
        params.put("model", "base.mselect");
        params.put("fun", "getsupplier");
        Log.i("goods", params.toString());
        new HttpConnection().postNet(params, new HttpCallBackListener() {
            @Override
            public void CallBack(String Response) {
                Log.i("login", Response);
                try {
                    JSONObject jsonObject = new JSONObject(Response);
                    JSONArray result = jsonObject.getJSONArray("result");
                    SPHelper.getInstance().putString(Constant.SUP_LIST, result.toString());
                } catch (JSONException e) {

                    e.printStackTrace();
                }
            }
        });


    }

    //下载客员工
    private void downCus() {
        Map<String, String> params = new HashMap<>();
        params.put("logid", SPHelper.getInstance().getString(Constant.USER_ID));
        params.put("sysid", SPHelper.getInstance().getString(Constant.SYS_ID));
        params.put("model", "base.mselect");
        params.put("fun", "getperson");
        Log.i("goods", params.toString());
        new HttpConnection().postNet(params, new HttpCallBackListener() {
            @Override
            public void CallBack(String Response) {
                Log.i("login", Response);
                try {
                    JSONObject jsonObject = new JSONObject(Response);
                    JSONArray result = jsonObject.getJSONArray("result");
                    SPHelper.getInstance().putString(Constant.CUS_LIST, result.toString());
                } catch (JSONException e) {

                    e.printStackTrace();
                }

            }
        });
    }

    private String startrq = GetData.getAnyTime(0);
    private String endrq = GetData.getAnyTime(0);

    private void searchBillfast() {
        showLodingDialog();
        Map<String, String> params = new HashMap<>();
        params.put("logid", SPHelper.getInstance().getString(Constant.USER_ID));
        params.put("sysid", SPHelper.getInstance().getString(Constant.SYS_ID));
        params.put("model", "report.Mreport");
        params.put("fun", "find_spxstop");

        params.put("page", "1");
        params.put("limit", "100");


        JSONObject object = new JSONObject();
        try {
            object.put("startrq", startrq);
            object.put("endrq", endrq);
            object.put("market", market);
            object.put("supid", supid);
            object.put("custid", custid);
            object.put("brandid", brandid);
            object.put("num", num);
            object.put("barcode", barcode);
            object.put("name", name);
            object.put("catid", cat);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        params.put("pds", object.toString());
        Log.i("login", params.toString());
        new HttpConnection().postNet(params, new HttpCallBackListener() {
            @Override
            public void CallBack(String Response) {
                Log.i("login", Response);
                try {
                    JSONObject jsonObject = new JSONObject(Response);
                    Log.i("login", jsonObject.getString("errMsg"));
                    JSONArray result = jsonObject.getJSONArray("result");
                    getResult(result);
                    count = jsonObject.getJSONObject("sumrow").getString("MLE");
                    je = jsonObject.getJSONObject("sumrow").getString("LSJJE");
                    Message message = Message.obtain();
                    message.what = 1;
                    handler.sendMessage(message);
                } catch (JSONException e) {

                    e.printStackTrace();
                }

            }
        });

    }

    List<GoodsTopBeen> topBeens = new ArrayList<>();

    private void getResult(JSONArray result) {
        topBeens.clear();
        for (int i = 0; i < result.length() - 1; i++) {
            try {
                JSONObject object = result.getJSONObject(i);
                GoodsTopBeen been = new Gson().fromJson(object.toString(), GoodsTopBeen.class);
                topBeens.add(been);
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }


    private List<SupBeen> fl_Beens = new ArrayList<>();//分类
    private List<SupBeen> pp_Beens = new ArrayList<>();//品牌
    private List<SupBeen> md_Beens = new ArrayList<>();//门店

    //下载分类
    private void down() {
        Map<String, String> params = new HashMap<>();
        params.put("logid", SPHelper.getInstance().getString(Constant.USER_ID));
        params.put("sysid", SPHelper.getInstance().getString(Constant.SYS_ID));
        params.put("model", "base.mselect");
        params.put("fun", "getlastgoodscat");
        Log.i("goods", params.toString());
        new HttpConnection().postNet(params, new HttpCallBackListener() {
            @Override
            public void CallBack(String Response) {
                Log.i("login", Response);
                try {
                    JSONObject jsonObject = new JSONObject(Response);
                    JSONArray result = jsonObject.getJSONArray("result");
                    fl_Beens = new Gson().fromJson(result.toString(), new TypeToken<List<SupBeen>>() {
                    }.getType());
                    fl_Beens.add(new BillUtil().getNewSup());

                } catch (JSONException e) {

                    e.printStackTrace();
                }
            }
        });


    }

    //下载
    private void downmd() {
        Map<String, String> params = new HashMap<>();
        params.put("logid", SPHelper.getInstance().getString(Constant.USER_ID));
        params.put("sysid", SPHelper.getInstance().getString(Constant.SYS_ID));
        params.put("model", "base.mselect");
        params.put("fun", "getlastmarket");
        Log.i("goods", params.toString());
        new HttpConnection().postNet(params, new HttpCallBackListener() {
            @Override
            public void CallBack(String Response) {
                Log.i("login", Response);
                try {
                    JSONObject jsonObject = new JSONObject(Response);
                    JSONArray result = jsonObject.getJSONArray("result");
                    md_Beens = new Gson().fromJson(result.toString(), new TypeToken<List<SupBeen>>() {
                    }.getType());

                    md_Beens.add(new BillUtil().getNewSup());
                } catch (JSONException e) {

                    e.printStackTrace();
                }
            }
        });


    }

    //下载
    private void downpp() {
        Map<String, String> params = new HashMap<>();
        params.put("logid", SPHelper.getInstance().getString(Constant.USER_ID));
        params.put("sysid", SPHelper.getInstance().getString(Constant.SYS_ID));
        params.put("model", "base.mselect");
        params.put("fun", "getlastgoodsbrand");
        Log.i("goods", params.toString());
        new HttpConnection().postNet(params, new HttpCallBackListener() {
            @Override
            public void CallBack(String Response) {
                Log.i("login", Response);
                try {
                    JSONObject jsonObject = new JSONObject(Response);
                    JSONArray result = jsonObject.getJSONArray("result");
                    pp_Beens = new Gson().fromJson(result.toString(), new TypeToken<List<SupBeen>>() {
                    }.getType());
                    pp_Beens.add(new BillUtil().getNewSup());
                } catch (JSONException e) {

                    e.printStackTrace();
                }

            }
        });
    }

    private void creatDialogCat(final List<String> ss) {

        new CircleDialog.Builder(this)
                .setItems(ss, new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        //分类
                        tv_fl.setText(ss.get(i));
                        cat = fl_Beens.get(i).getId();

                    }
                })
                .setGravity(Gravity.CENTER)
                .setWidth(0.5f)
                .show();
    }

    private void creatDialogBrand(final List<String> ss) {

        new CircleDialog.Builder(this)
                .setItems(ss, new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        //品牌
                        tv_pp.setText(ss.get(i));
                        brandid = pp_Beens.get(i).getId();

                    }
                })
                .setGravity(Gravity.CENTER)
                .setWidth(0.5f)
                .show();
    }

    private void creatDialogMarket(final List<String> ss) {

        new CircleDialog.Builder(this)
                .setItems(ss, new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        //门店
                        tv_md.setText(ss.get(i));
                        market = md_Beens.get(i).getId();
                    }
                })
                .setGravity(Gravity.CENTER)
                .setWidth(0.5f)
                .show();
    }

    private void showmd(View v) {
        List<String> strings = new ArrayList<>();
        strings.clear();
        for (SupBeen been : md_Beens) {
            strings.add(been.getName());
        }
        View contentView = LayoutInflater.from(this).inflate(R.layout.dialog_yyy, null);
        final PopupWindow yyyPopupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        yyyPopupWindow.setBackgroundDrawable(new BitmapDrawable());
        yyyPopupWindow.setFocusable(true);
        contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        int height = contentView.getMeasuredHeight();
        int width = contentView.getMeasuredWidth();
        int[] location = new int[2];
        v.getLocationOnScreen(location);
//        yyyPopupWindow.showAtLocation(v, Gravity.NO_GRAVITY, (location[0] + v.getWidth() / 2) - width / 2, location[1]);
        yyyPopupWindow.showAsDropDown(v);

        ListView listView = contentView.findViewById(R.id.yyy_list);
        listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strings));
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                tv_md.setText(md_Beens.get(position).getName());
                market = md_Beens.get(position).getId();
                yyyPopupWindow.dismiss();
            }
        });
    }

    private void showfl(View v) {
        List<String> strings = new ArrayList<>();
        strings.clear();
        for (SupBeen been : fl_Beens) {
            strings.add(been.getName());
        }
        View contentView = LayoutInflater.from(this).inflate(R.layout.dialog_yyy, null);
        final PopupWindow yyyPopupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        yyyPopupWindow.setBackgroundDrawable(new BitmapDrawable());
        yyyPopupWindow.setFocusable(true);
        contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        int height = contentView.getMeasuredHeight();
        int width = contentView.getMeasuredWidth();
        int[] location = new int[2];
        v.getLocationOnScreen(location);
//        yyyPopupWindow.showAtLocation(v, Gravity.NO_GRAVITY, (location[0] + v.getWidth() / 2) - width / 2, location[1]);
        yyyPopupWindow.showAsDropDown(v);

        ListView listView = contentView.findViewById(R.id.yyy_list);
        listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strings));
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                tv_fl.setText(fl_Beens.get(position).getName());
                cat = fl_Beens.get(position).getId();
                yyyPopupWindow.dismiss();
            }
        });
    }

    private void showpp(View v) {
        List<String> strings = new ArrayList<>();
        strings.clear();
        for (SupBeen been : pp_Beens) {
            strings.add(been.getName());
        }
        View contentView = LayoutInflater.from(this).inflate(R.layout.dialog_yyy, null);
        final PopupWindow yyyPopupWindow = new PopupWindow(contentView, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        yyyPopupWindow.setBackgroundDrawable(new BitmapDrawable());
        yyyPopupWindow.setFocusable(true);
        contentView.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
        int height = contentView.getMeasuredHeight();
        int width = contentView.getMeasuredWidth();
        int[] location = new int[2];
        v.getLocationOnScreen(location);
//        yyyPopupWindow.showAtLocation(v, Gravity.NO_GRAVITY, (location[0] + v.getWidth() / 2) - width / 2, location[1]);
        yyyPopupWindow.showAsDropDown(v);

        ListView listView = contentView.findViewById(R.id.yyy_list);
        listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strings));
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                tv_pp.setText(pp_Beens.get(position).getName());
                brandid = pp_Beens.get(position).getId();
                yyyPopupWindow.dismiss();
            }
        });
    }

    private void showDate(final TextView tv) {
        final Calendar c = Calendar.getInstance();
        DatePickerDialog dialog = new DatePickerDialog(GoodsReportActivity.this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

                c.set(year, monthOfYear, dayOfMonth);
                tv.setText(DateFormat.format("yyyy-MM-dd", c));
            }
        }, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
        dialog.show();
    }
}
